package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Cases;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.LossItem;
import cn.wolfcode.crm.mapper.AccidentMapper;
import cn.wolfcode.crm.mapper.CasesMapper;
import cn.wolfcode.crm.mapper.LossItemMapper;
import cn.wolfcode.crm.mapper.PolicyMapper;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICasesService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CasesServiceImpl implements ICasesService {

    @Autowired
    private CasesMapper casesMapper;
    @Autowired
    private AccidentMapper accidentMapper;
    @Autowired
    private PolicyMapper policyMapper;
    @Override
    public PageResult query(QueryObject qo) {
        Integer total= casesMapper.queryForCount(qo);
        List<Cases> rows =  casesMapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    @Override
    public void save(Cases cases) {
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        cases.setEmployee(employee);
        cases.setState(Cases.STATE_NORMAL);
        casesMapper.insert(cases);
        accidentMapper.insertCaseId(cases.getId());

    }

    @Override
    public void update(Cases cases) {
        casesMapper.updateByPrimaryKey(cases);
    }

    @Override
    public String searchStateBypolicySn(String policySn) {
       int state= policyMapper.getStateBypolicySn(policySn);
       String stateResult=null;
       if (state==0){
           stateResult="暂存";
       }else if (state==1){
           stateResult="未审核";
       } else if (state==2){
        stateResult="审核未交费";
       } else if (state==3){
        stateResult="拒保";
       }
       return stateResult;
    }


}
